package top.hidoctor.auscultation.net.p;

import android.content.Context;

import com.tang.base.BaseResponse;
import com.tang.base.NetMapEncryptUtils;
import com.tang.base.PatchRequest;
import com.tang.net.SuccessConsumer;
import com.tang.user.UserInfoManager;

import java.util.HashMap;
import java.util.Map;

import io.reactivex.functions.Consumer;
import top.hidoctor.auscultation.bean.LableBean;
import top.hidoctor.auscultation.net.PresenterWrapper;
import top.hidoctor.auscultation.net.c.LabelDetailContract;

public class LabelDetailPresenter extends PresenterWrapper<LabelDetailContract.View> implements LabelDetailContract.Presenter {


    public LabelDetailPresenter(Context context, LabelDetailContract.View view) {
        super(context, view);
    }

    @Override
    public void getDetail(String id, boolean isUser) {
        mView.showLoading();
        Map<String, Object> map = new NetMapEncryptUtils("").getMaps();
        if (isUser) map.put("userId", UserInfoManager.getInstance().getUserInfo().getUserId());
        add(mService.labelDetail(id, map)
                .compose(this.<LableBean>getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<LableBean>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<LableBean> response) {
                        mView.hideLoading();
                        mView.getDetailSuc(response.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }

    @Override
    public void updateLabel(String id) {
        mView.showLoading();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("userId", UserInfoManager.getInstance().getUserInfo().getUserId());

        Map<String, Object> map = new NetMapEncryptUtils("").getMaps();

        add(mService.updataLabel(id, map1, new PatchRequest(map).getRequestBody())
                .compose(this.getTransformer())
                .subscribe(new SuccessConsumer<BaseResponse<Object>>(mView, mContext) {
                    @Override
                    public void onSuccess(BaseResponse<Object> response) {
                        mView.hideLoading();
                        mView.updateLabelSuc();

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        throwable.printStackTrace();
                    }
                }));
    }
}
